import { createSSRApp } from 'vue';
import App from './App.vue';
import { getUserInfoApi, getCustomerInfoApi } from '@/api/user';
import { USER_ACCESS_TOKEN } from '@/libs/config.js';
import { setStorageUserInfo } from '@/utils/utils.js';
import uniIcons from '@dcloudio/uni-ui/lib/uni-icons/uni-icons.vue';
import EmptyData from '@/components/EmptyData.vue';
import PageLoading from '@/components/PageLoading.vue';
import DataLoading from '@/components/DataLoading.vue';
import NotLogin from '@/components/NotLogin.vue';
import NotCustomer from '@/components/NotCustomer.vue';
import './main.css';

// 获取用户信息
async function openInit() {
  const token = uni.getStorageSync(USER_ACCESS_TOKEN);
  if (token) {
    const { code, data } = await getUserInfoApi();
    if (code == 200) {
      const {
        code: code2,
        data: { list = [] },
      } = await getCustomerInfoApi();
      if (code2 == 200) {
        data.customer_id = list?.[0]?.id ?? '';
        data.expiration_time = list?.[0]?.expiration_time ?? '';
      }
      setStorageUserInfo(data);
    }
  }
}
openInit();
export function createApp() {
  const app = createSSRApp(App);
  app
    .component('uniIcons', uniIcons)
    .component('EmptyData', EmptyData)
    .component('PageLoading', PageLoading)
    .component('DataLoading', DataLoading)
    .component('NotLogin', NotLogin)
    .component('NotCustomer', NotCustomer);
  return {
    app,
  };
}
